/**
 * @author Zhang Jing
 */
package club325.webchat.database.hibernate.implement;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import club325.webchat.database.hibernate.dao.HibernateUtil;
import club325.webchat.database.hibernate.dao.MemberDAO;
import club325.webchat.database.hibernate.domain.Member;

/**
 * @author Sider
 *
 */
public class MemberImplement implements MemberDAO
{
	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#saveMember(club325.webchat.database.hibernate.domain.Member)
	 */
	public void saveMember(Member entity)
	{
		HibernateUtil.add(entity);
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#remove(club325.webchat.database.hibernate.domain.Member)
	 */
	public void remove(Member entity)
	{
		HibernateUtil.delete(entity);
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#findMemberbyID(int)
	 */
	public Member findMemberbyID(int id)
	{
		return (Member)HibernateUtil.get(Member.class, id); 
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#findMemberbyMemberID(java.lang.String)
	 */
	public Member findMemberbyMemberID(String memberID)
	{
		String HQL = "FROM Member AS members WHERE members.memberID = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, memberID);
		return (Member)q.uniqueResult();
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#findMemberbyadmin(boolean)
	 */
	@SuppressWarnings("unchecked")
	public List<Member> findMemberbyadmin(boolean ad)
	{
		String HQL = "FROM Member AS members WHERE members.admin = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setBoolean(0, ad);
		return q.list();
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#findMemberbypunish(int)
	 */
	@SuppressWarnings("unchecked")
	public List<Member> findMemberbypunish(int punish)
	{
		String HQL = "FROM Member AS members WHERE members.punish = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setInteger(0, punish);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public Set<String> getMemberIDSet()
	{
		String HQL = "SELECT members.memberID FROM Member as members";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		return (new HashSet<String>(q.list()));
	}
	
	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.MemberDAO#getMemberIDSetbyadmin(int)
	 */
	@SuppressWarnings("unchecked")
	public Set<String> getMemberIDSetbyadmin(boolean ad) {
		String HQL = 
			"SELECT members.memberID " +
			"FROM Member as members " +
			"WHERE members.type = ?";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		if(ad)
		{
			q.setInteger(0, 1);
		}
		else
		{
			q.setInteger(0,0);
		}
		return (new HashSet<String>(q.list()));
	}

	public void UpdateMember(Member entity)
	{
		HibernateUtil.Update(entity);
	}
}
